﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GlxRRSDB.ReportDataSetTableAdapters;
using GlxRRSDB;
using GlxRRSMain.Utils;

namespace GlxRRSMain.UserControls
{
    public partial class ImportSessionDataControl : UserControl
    {
        private tblSessionTableAdapter x_tblSessionTableAdapter = new tblSessionTableAdapter();
        private System.Data.DataSet DtSet;
        //
        private const int IDX_CINEMA_NAME = 0;
        private const int IDX_YEAR = 1;
        private const int IDX_MONTH = 2;
        private const int IDX_DATE = 3;
        private const int IDX_SCREEN = 4;
        private const int IDX_SESSION_ID = 5;
        private const int IDX_ADMIT = 6;
        //
        public ImportSessionDataControl()
        {
            InitializeComponent();
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            DataTable dtTable = DtSet.Tables[0];
            int cachedCinemaID = 0;
            int cachedScreen = 1;
            //
            DateTime cachedDateTime = new DateTime();
            for (int i = 1; i < dtTable.Rows.Count; i++)
            {
                string cinemaName = dtTable.Rows[i][IDX_CINEMA_NAME].ToString();
                if(cinemaName.ToLower().Contains("nguyen du")) {
                    cachedCinemaID = 1001;
                } else if(cinemaName.ToLower().Contains("nguyen trai")) {
                    cachedCinemaID = 1002;
                } else if(cinemaName.ToLower().Contains("tan binh")) {
                    cachedCinemaID = 1003;
                }
                //
                string strDate = dtTable.Rows[i][IDX_DATE].ToString();
                if (strDate.ToLower().Contains("total"))
                {
                    continue;
                }
                if (strDate.ToLower().Length > 7)
                {
                    cachedDateTime = DateTime.Parse(strDate); ;
                }
                string strSessionID = dtTable.Rows[i][IDX_SESSION_ID].ToString();
                if (strSessionID.Length < 1) continue;
                int sessionID = int.Parse(strSessionID);
                int admits = int.Parse(dtTable.Rows[i][IDX_ADMIT].ToString());
                int[] screen = new int[] { 0, 0, 0, 0, 0 };
                string strScreen = dtTable.Rows[i][IDX_SCREEN].ToString();
                if (strScreen.ToLower().Contains("total"))
                {
                    continue;
                }
                if (strScreen.Length > 0)
                {
                    cachedScreen = int.Parse(strScreen);
                }
                if (cachedScreen> 0 && cachedScreen <= 5)
                {
                    cachedScreen--;
                }
                screen[cachedScreen] = admits;
                //
                ReportDataSet.tblTargetDataTable t = new ReportDataSet.tblTargetDataTable();
                x_tblSessionTableAdapter.Insert(cachedCinemaID, screen[0], screen[1], screen[2], screen[3], screen[4], cachedDateTime);
            }
            MessageBox.Show("Imported total:" + dtTable.Rows.Count + " rows");
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            importFileDialog.Filter = "Excel (.xlsx)|*.xlsx|All Files (*.*)|*.*";
            importFileDialog.FilterIndex = 1;
            importFileDialog.Multiselect = false;

            // Call the ShowDialog method to show the dialog box.
            DialogResult userClickedOK = importFileDialog.ShowDialog();

            // Process input if the user clicked OK.
            if (userClickedOK == DialogResult.OK)
            {
                // Open the selected file to read.
                textBox1.Text = importFileDialog.FileName;
                loadDataGrid(textBox1.Text);
            }

        }
        private void loadDataGrid(string path)
        {
            try
            {
                DtSet = FileHelper.getDataSetFromExcel(path);
                dataGridView1.DataSource = DtSet.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
